package com.stone.business.gzp.action;

/**
 * <pre>
 * 工作票统计记录
 * @author 
 * </pre>
 */

import java.util.HashMap;
import java.util.List;

import com.stone.business.gzjl.pojo.TabGzjl;
import com.stone.business.gzp.pojo.TGzptjjl;
import com.stone.business.gzrwd.service.ZbjlCommonService;
import com.stone.business.plan.pojo.TPlan;
import com.stone.business.plan.pojo.TPlanBz;
import com.stone.para.IConstant;
import com.stone.sys.pojo.TDictionary;
import com.stone.sys.pojo.TUser;
import com.stone.sys.service.DictionaryService;
import com.stone.sys.service.UserService;
import com.stone.utils.DateFormatTool;
import com.stone.utils.StringUtils;
import com.stone.utils.seq.SeqUtil;

import frm.ssh.action.BaseAction;
import frm.ssh.exception.AppException;
import frm.ssh.util.SpringUtil;

public class GzptjjlAction extends BaseAction {
	
	private static final long serialVersionUID = -4647594073681054487L;

	private ZbjlCommonService myService;
	
	// 工作票
	private TGzptjjl entity;
	
	private String id;
	private String planid;
	private String gzpno;
	// 工作票通知类型字典
	private List<TDictionary> gzptzlxList;
	// 设备类型
	private List<TDictionary> sblxList;
	// 调度员
	private List<TDictionary> ddyList;
	// 通知单位
	private List<TDictionary> tzdwList;
	
	// yg：从预告过来 gzp:从工作票过来
	private String comefrom;
	// 预告对应工作票的列表
	private List<TGzptjjl> gzpList;
	// 预告ID
	private String ygid;
	private String ph;
	/**
	 * 进入详细页面
	 * @return
	 * @throws AppException
	 */
	public String initDetail() throws AppException {
		// 从工作任务单过来
		if (!StringUtils.isNullOrBlankFull(gzpno)) {
			this.entity = (TGzptjjl)myService.findFirst("from TGzptjjl where ph = '"+ gzpno + "'");
			TPlan plan = (TPlan)myService.findById(Long.parseLong(planid), TPlan.class.getName());
			List<TPlanBz>  planbzList = myService.findListByHql(" from TPlanBz where delflag='0' and planid = '"+ planid + "'"  + " order by id");
			String username = "";
			String user = "";
			for(TPlanBz planBz:planbzList){
				username += planBz.getFzrname() + ","+planBz.getCyname()+ ",";
				user += planBz.getFzrid() + ","+planBz.getCyid()+ ",";
			}
			if (username.endsWith(",")) {
				user = user.substring(0, user.length() -1);
				username = username.substring(0, username.length() -1);
			}
			this.entity.setGzfzr(plan.getFzr());
			this.entity.setGzfzrname(plan.getResponsible());
			this.entity.setGzfzrbd(user);
			this.entity.setGzfzrbdname(username);
			// 过滤掉重复的成员
			List<TUser> l  = myService.findListByHql("from TUser where id in (" + user + ")");
			
			String usernames = "";
			String users = "";
			for (TUser u : l) {
				usernames += u.getName() + ",";
				users += u.getId() + ",";
			}
			
			if (usernames.endsWith(",")) {
				usernames = usernames.substring(0, usernames.length() -1);
				users = users.substring(0, users.length() -1);
			}
			this.entity.setGzfzrbd(users);
			this.entity.setGzfzrbdname(usernames);
			
			super.getSession().setAttribute("planid", planid);
			// 修改进入
		}else if (!"0".equals(id)) {
			
			this.entity = (TGzptjjl)myService.findById(Long.parseLong(id), TGzptjjl.class.getName());
		// 新建进入
		} else {
			
			this.entity = new TGzptjjl();
			this.entity.setState(super.getSearchBean().getCon1());
			
			entity.setCjr(((TUser) super.getSession().getAttribute(IConstant.SESSION_TUSER)).getName());
			entity.setCjrid(((TUser) super.getSession().getAttribute(IConstant.SESSION_TUSER)).getId());

		}
		
		// 获得字典
		getDic();
		
		return SUCCESS;
	}
	
	/**
	 * 保存
	 * @return
	 * @throws AppException
	 */
	public String save() throws AppException {
		
		String sysdate = myService.getSysdate("yyyy-MM-dd HH24:mi");
		
		// 新建时
		if (entity.getId()==0) {
			
			//List<HashMap<String, Object>> list = myService.findListBySql("select MAX(ph) as ph from TAB_GZPTJJL where ph like '" + entity.getPh().substring(0,6) + "%'");
			
//			// 重新取得票号
//			if (list.get(0).get("ph") != null) {
//				entity.setPh(String.valueOf(Long.parseLong((String)list.get(0).get("ph")) + 1 ));
//			}
			
			Long oldid = entity.getId();
			//entity.setCjr(((TUser) super.getSession().getAttribute(IConstant.SESSION_TUSER)).getName());
			//entity.setCjrid(((TUser) super.getSession().getAttribute(IConstant.SESSION_TUSER)).getId());
			entity.setCjsj(DateFormatTool.formatToDate(sysdate,true));
			myService.save(entity);	
			
			// 从预告过来的需要将ID更改为新建tab时从序列取得的ID号
			if ("yg".equals(comefrom)) {
				myService.updateByHQL("update TGzptjjl set id = ? where id = ?", new Object[]{oldid, entity.getId()});
				this.entity.setId(oldid);
			}
			
		} else {
//			by wangtao add20110330
			if(entity.getXkgzsj()!=null&&!StringUtils.isNullOrBlankFull(entity.getGzxkdd())&&!StringUtils.isNullOrBlankFull(entity.getXkszbyname())&&StringUtils.isNullOrBlankFull(entity.getCjr1())){
				entity.setCjr1(((TUser) super.getSession().getAttribute(IConstant.SESSION_TUSER)).getName());
				entity.setCjrid1(((TUser) super.getSession().getAttribute(IConstant.SESSION_TUSER)).getId());
				entity.setCjsj1(DateFormatTool.formatToDate(sysdate,true));
				
			}
//			by wangtao add20110330
			if(entity.getGzzjsj()!=null&&!StringUtils.isNullOrBlankFull(entity.getZjshbddname())&&!StringUtils.isNullOrBlankFull(entity.getZjszbyname())&&StringUtils.isNullOrBlankFull(entity.getCjr2())){
				entity.setCjr2(((TUser) super.getSession().getAttribute(IConstant.SESSION_TUSER)).getName());
				entity.setCjrid2(((TUser) super.getSession().getAttribute(IConstant.SESSION_TUSER)).getId());
				entity.setCjsj2(DateFormatTool.formatToDate(sysdate,true));
				
			}
	
			entity.setXgr(((TUser) super.getSession().getAttribute(IConstant.SESSION_TUSER)).getName());
			entity.setXgrid(((TUser) super.getSession().getAttribute(IConstant.SESSION_TUSER)).getId());
			entity.setXgsj(DateFormatTool.formatToDate(sysdate,true));
			myService.updateGzptjjl(entity , (String)super.getSession().getAttribute("planid"));
			
			//super.getSession().removeAttribute("XS_GZRWDRWID");
		}
		
		// 获得字典
		getDic();

		super.setInfo("保存成功");
		
		return SUCCESS;
	}
	
	/**
	 * 删除
	 * @return
	 * @throws AppException
	 */
	public String delete() throws AppException {
		
		myService.doDelAll(id, "TGzptjjl");
		
		return "DEL-SUCCESS";
		
	}
	
	/**
	 * 查询字典项目(从运行工区数据库)
	 */
	public void getDic() {
		
		// 工作票通知类型
		UserService dicService = (UserService) SpringUtil.getInstance().getBean("userServiceDIMIS");
		
		gzptzlxList = dicService.findListByHql(" from TDictionary d where d.itemType='TZLX' order by sortid");
		// 设备类型
		sblxList = dicService.findListByHql(" from TDictionary d where d.itemType='SBLX'  order by sortid");
		
		// 调度员
		ddyList = dicService.findListByHql(" from TBzUser where userstate != '1' and usertype = (select id from TDictionary where itemType = 'BZ' and itemId = '01')");
		
		// 通知单位
		tzdwList = dicService.findListByHql(" from TDictionary d where d.itemType='BZ'  order by sortid");
		
		
	}

	/**
	 * 从预告对应工作票列表
	 */
	public String getYgGzpList() throws AppException {
		
		gzpList = myService.findListByHql(" from TGzptjjl where gzygid = " + this.ygid);
		
		return "GZYG-JL-SUCCESS";
	}
	
	/**
	 * 从预告对应工作票列表删除工作票
	 * @return
	 * @throws AppException
	 */
	public String deleteYgjl() throws AppException {
		
		myService.doDelAll(id, TGzptjjl.class.getName());
		
		gzpList = myService.findListByHql(" from TGzptjjl where gzygid = " + this.ygid);
		
		return "GZYG-JL-SUCCESS";
		
	}
	
	/**
	 * 起草工作票时，先获得工作票表ID，作为tab的id
	 */
	public void getNewGzpId() throws Exception{
		
		super.getResponse().getWriter().print("{gzpid:'" + SeqUtil.getNextKey("SEQ_XSQK") + "'}");
	}
	
	/**
	 * 起草工作票时，先获得工作票表ID，作为tab的id
	 */
	public void getNewPh() throws Exception {
	
		List<HashMap<String, Object>> list = myService.findListBySql("select MAX(ph) as ph from TAB_GZPTJJL where ph like '" + this.ph.substring(0,6) + "%'");
		
		// 票号取得
		this.ph = String.valueOf(Long.parseLong((String)list.get(0).get("ph")) + 1 );

		super.getResponse().getWriter().print("{ph:'" + this.ph + "'}");
	}
	
	
	/**
	 * 工作票提前办理终结
	 * @return
	 * @throws AppException
	 */
	public void gzptqzj() throws Exception {
		
		myService.updateByHQL("update TGzptjjl set gzrwdconfirm3 = '1' where id = ?", new Object[]{Long.parseLong(this.id)});
		
		super.getResponse().getWriter().print("{m:1}");
		
	}
	/**
	 * 取消工作票记录 
	 *  操作人为登陆用户  20110722 by wangtao add
	 * @return
	 */
	public void cancelGzptjjl() throws Exception {
		
		// 系统时间
		String sysdate = myService.getSysdate("yyyy-MM-dd HH24:mi");
		
		myService.cancelGzptjjl(this.id, super.getRequest().getParameter("qxtzr"), 
				((TUser) super.getSession().getAttribute(IConstant.SESSION_TUSER)).getId(),
				((TUser) super.getSession().getAttribute(IConstant.SESSION_TUSER)).getName(), DateFormatTool.formatToDate(sysdate,true));
				
	}
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public void setMyService(ZbjlCommonService myService) {
		this.myService = myService;
	}

	public List<TDictionary> getGzptzlxList() {
		return gzptzlxList;
	}

	public TGzptjjl getEntity() {
		return entity;
	}

	public void setEntity(TGzptjjl entity) {
		this.entity = entity;
	}

	public List<TDictionary> getSblxList() {
		return sblxList;
	}

	public String getComefrom() {
		return comefrom;
	}

	public void setComefrom(String comefrom) {
		this.comefrom = comefrom;
	}

	public List<TGzptjjl> getGzpList() {
		return gzpList;
	}

	public String getYgid() {
		return ygid;
	}

	public void setYgid(String ygid) {
		this.ygid = ygid;
	}

	public List<TDictionary> getDdyList() {
		return ddyList;
	}

	

	public String getPh() {
		return ph;
	}

	public void setPh(String ph) {
		this.ph = ph;
	}

	
	public List<TDictionary> getTzdwList() {
		return tzdwList;
	}

	public String getPlanid() {
		return planid;
	}

	public void setPlanid(String planid) {
		this.planid = planid;
	}

	public String getGzpno() {
		return gzpno;
	}

	public void setGzpno(String gzpno) {
		this.gzpno = gzpno;
	}


}
